package com.itcast.springboot.waste_recovery.controller;


import com.itcast.springboot.waste_recovery.common.CommonResult;
import com.itcast.springboot.waste_recovery.common.CommonResultCode;
import com.itcast.springboot.waste_recovery.domain.UserAddress;
import com.itcast.springboot.waste_recovery.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/waste_recovery/user-address")
@Slf4j
@Api(tags = "用户地址类接口")
public class UserAddressController {
    @Resource
    private UserAddressService userAddressService;

    @GetMapping("/customer/addressesManage/{user_id}")
    @ApiOperation(value = "地址管理（获取当前用户所有地址）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult getAddressesManage(@PathVariable(value = "user_id") Integer userId){
        List<UserAddress> userAddresses = userAddressService.getUserAddress(userId);
        return new CommonResult<>(CommonResultCode.CommonSuccess, userAddresses);
    }

    @PutMapping("/customer/address_default/{user_id}/{address_id}")
    @ApiOperation(value = "设置默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id"),
            @ApiImplicitParam(name = "address_id", value = "地址id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult updateAddressDefault(@PathVariable(value = "user_id") Integer userId,
                                               @PathVariable(value = "address_id") Integer addressId){
        Integer result = userAddressService.updateAddressDefault(userId, addressId);
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @PostMapping("/customer/{user_id}")
    @ApiOperation(value = "添加地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult insertAddress(@PathVariable(value = "user_id") Integer userId,
                                      @RequestBody UserAddress userAddress){
        userAddress.setAddressUserId(userId);
        Integer result = userAddressService.insertAddress(userAddress);
        if (result <= 0){
            return CommonResult.CommonFail();
        }
        return new CommonResult(CommonResultCode.CommonSuccess, result);
    }



    @PutMapping("/customer/{address_id}")
    @ApiOperation(value = "修改地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "address_id", value = "地址id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult updateAddress(@PathVariable(value = "address_id") Integer addressId,
                                      @RequestBody UserAddress userAddress){
        userAddress.setAddressId(addressId);
        Integer result = userAddressService.updateAddress(userAddress);
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @DeleteMapping("/customer/{address_id}")
    @ApiOperation(value = "删除地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "address_id", value = "地址id")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult updateAddress(@PathVariable(value = "address_id") Integer addressId){
        Integer result = userAddressService.deleteAddress(addressId);
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }
}

